<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2018/10/10
 * Time: 20:26
 */

namespace Consumer\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\OrderModel;
use Common\Model\SaleMemberModel;

class SaleMemberController extends AdminbaseController
{
    private $sale_member_model;
    private $order_model;

    public function __construct()
    {
        parent::__construct();
        $this->sale_member_model    = new SaleMemberModel();
        $this->order_model          = new OrderModel();
    }

    public function lists() {
        $where = [];

        $count = $this->sale_member_model->where($where)->count();

        $page = $this->page($count, C('PAGE_NUMBER'));

        $result = $this->sale_member_model->limit($page->firstRow, $page->listRows)->select();

        foreach ($result as $k => $v) {
            $result[$k]['total_price'] = $this->order_model->where(['sale_code' => $v['sale_code'], 'status' => ['in', [OrderModel::STATUS_PAY_SUCCESS,OrderModel::STATUS_WAIT_COMMENT,OrderModel::STATUS_COMPLETE]]])->sum('total_price');
        }
//        dump($this->order_model->getLastSql());
        $this->assign('list', $result);

        $this->display();
    }

    public function add() {
        $post = I('post.');
        if($this->sale_member_model->where(['sale_code' => $post['sale_code']])->find()) {
            $this->error('该优惠码已存在，请重新填写');
        }
        $post['update_time'] = getTime();
        if($this->sale_member_model->add($post)) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    public function edit() {
        $post = I('post.');
        $post['update_time'] = getTime();
        if($this->sale_member_model->save($post)) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    public function order($sale_code) {
        $where['a.sale_code'] = $sale_code;
        $where['a.status'] = ['in', [OrderModel::STATUS_PAY_SUCCESS,OrderModel::STATUS_WAIT_COMMENT,OrderModel::STATUS_COMPLETE]];
        $count = $this->order_model
            ->alias('a')
            ->join('INNER JOIN he_member as b ON a.mid = b.id')
            ->where($where)
            ->count();
        $page = $this->page($count, C('PAGE_NUMBER'));
        $ordersTotal = $this->order_model
            ->alias('a')
            ->join('INNER JOIN he_member as b ON a.mid = b.id')
            ->field('sum(a.order_price) total_price')
            ->where($where)
            ->find();
        $allTotal=0;
        if ($ordersTotal) {
            $allTotal=$ordersTotal["total_price"];
        }
        $orders = $this->order_model
            ->alias('a')
            ->join('INNER JOIN he_member as b ON a.mid = b.id')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->field('a.*, b.username, b.nickname')
            ->where($where)
            ->order('a.pay_time desc,a.create_time desc')
            ->select();

        $dblTotalPrice=0.0;
        foreach($orders as $k => $v) {
            $result[$k]['str_manage'] .= '<a class="js-ajax-btn-dialog" href="' . U('Order/details', ['id' => $v['id']]) . '">订单详情</a>';
            if ($v['status'] == OrderModel::STATUS_PAY_SUCCESS) {
                $result[$k]['str_manage'] .= ' | ';
                $result[$k]['str_manage'] .= '<a data-toggle="modal" data-target="#myModal"  class="add_ext"  onclick=""   name="' . $v['id'] . '">发货</a>';
            }

            $address = json_decode($v['address'], true);
            $lists .= '<tr>
                <td>' . ($k+1) . '</td>
                <td>' . $v['order_sn'] . '</td>
                <td>' . $v['username'] . '</td>
                <td>' . $v['nickname'] . '</td>
                <td>' . $v['total_price'] . '</td>
                <td>' . $v['cut_price'] . '</td>
                <td>' . $v['order_price'] . '</td>
                <td>' . $v['freight'] . '</td>
                <td>' . $this->order_model->getUserStatusString($v['status']) . '</td>
                <td>' . $this->order_model->payTypetoString($v['pay_type']) . '</td>
                <td>' . '地址：'.$address['province'].'/'.$address['city'].'/'.$address['county'].'/'.$address['address']. '/收货人：'.$address['fullname']. '/电话：'.$address['phone'] . '</td>
                <td>' . $v['pay_time'] . '</td>
                <td>' . $v['create_time'] . '</td>
                
            </tr>';
            $dblTotalPrice+=$v['order_price']*1.0;
        }

        $lists .= '<tr>
                <td><span style="color:red;">本页合计</span></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td><span style="color:red;">' . sprintf("%.2f", $dblTotalPrice) . '</span></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>';
        $lists .= '<tr>
                <td><span style="color:red;">所有总合计</span></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td><span style="color:red;">' . sprintf("%.2f", $allTotal) . '</span></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>';
        //获取省级区域
//        $province_region = $this->agency_model->getProvincetoStr($province_code);
//        $city_region = $this->agency_model->getCitytoStr($province_code,$city_code);
//        $country_region = $this->agency_model->getCitytoStr($city_code,$country_code);
//        $this->assign('province',$province_region);
//        $this->assign('city',$city_region);
//        $this->assign('country',$country_region);

        $this->assign('lists', $lists);
        $this->assign('statusOption', $this->order_model->getStatusOption(I('status')));
//        $this->assign('statusOptionChoice', $this->order_model->getStatusOption());
        $this->assign('formget', I(''));
        $this->assign("Page", $page->show());
        $this->display();
    }

}